package com.spark.actions.fin.gld;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.spark.core.Message;
import com.spark.core.SparkContext;
import com.spark.core.SparkException;
import com.spark.dao.SessionFactory;
import com.spark.tools.Logger;

@SuppressWarnings("unchecked")
public class JournalApproveAction extends JournalAction {
	
	private static final String SQL_JOURNAL_APPROVE_SELECT = "gld_journal.approveSelect";
	private static final String SQL_JOURNAL_APPROVE = "gld_journal.approve";
	private static final String SQL_JOURNAL_UNAPPROVE = "gld_journal.unApprove";
	private static final String SQL_JOURNAL_SELECT_APPROVE = "gld_journal.selectApproveList";
	
	
	private static final String ERROR_CODE_JOURNAL_APPROVE_SELECT = "JOURNAL_ACTION_APPROVE_SELECT";
	private static final String ERROR_CODE_JOURNAL_APPROVE = "JOURNAL_ACTION_APPROVE";
	private static final String ERROR_CODE_JOURNAL_UNAPPROVE = "JOURNAL_ACTION_UNAPPROVE";
	
	
	
	/**
	 * 凭证审核查询
	 * @param context
	 * @throws SparkException
	 */
	public void approveSelect(SparkContext context) throws SparkException{
		selectJournals(context,SQL_JOURNAL_APPROVE_SELECT,ERROR_CODE_JOURNAL_APPROVE_SELECT);			
	}
	
	
	/**
	 * 凭证审核
	 * @param context
	 * @throws SparkException
	 */
	public void approveJournal(SparkContext context) throws SparkException {
		Message message = new Message(true);
		SqlSession sqlSession = SessionFactory.getSession(false);
		try{
			Map requestData = getRequestData(context);
			List list = (List)requestData.get(PARAMETER);
			if(list != null && list.size() > 0){
				Iterator it = list.iterator();
				Integer userId = getUserId(context);
				String userName = getUserName(context);
				while(it.hasNext()){
					Map head = (Map)it.next();
					head.put(FIELD_HEAD_APPROVED_BY, userId);
					head.put(FIELD_HEAD_APPROVED_BY_NAME, userName);
					head.put(FIELD_HEAD_STATUS, DEFAULT_STATUS_APPROVED);
					head.put("DEFAULT_STATUS_SIGNED", DEFAULT_STATUS_SIGNED);
					sqlSession.update(SQL_JOURNAL_APPROVE,head);
				}
				sqlSession.commit();
				//返回状态 审核人和审核日期等信息
				Map m = new HashMap();
				m.put(LIST, list);
				List result = sqlSession.selectList(SQL_JOURNAL_SELECT_APPROVE,m);
				it = list.iterator();
				while(it.hasNext()){
					Map head = (Map)it.next();
					Iterator rit = result.iterator();
					while(rit.hasNext()){
						Map rh = (Map)rit.next();
						if(rh.get(FIELD_HEAD_JOURNAL_HEADER_ID).equals(head.get(FIELD_HEAD_JOURNAL_HEADER_ID))){
							head.putAll(rh);
							break;
						}
					}
				}
				message.setResult(list);
			}
			writeMessage(context, message);
		}catch(Exception e){
			Integer errorId = Logger.log(ERROR_CODE_JOURNAL_APPROVE, context, e);
			throw new SparkException(errorId);
		}finally {
			SessionFactory.closeSession(sqlSession);
		}
	}
	
	
	/**
	 * 反审核凭证
	 * @param context
	 * @throws SparkException
	 */
	public void unApproveJournal(SparkContext context) throws SparkException {
		Message message = new Message(true);
		SqlSession sqlSession = SessionFactory.getSession(false);
		try{
			Map requestData = getRequestData(context);
			List list = (List)requestData.get(PARAMETER);
			if(list != null && list.size() > 0){
				Iterator it = list.iterator();
				while(it.hasNext()){
					Map head = (Map)it.next();
					head.put(FIELD_HEAD_APPROVED_BY, null);
					head.put(FIELD_HEAD_APPROVED_BY_NAME, null);
					head.put(FIELD_HEAD_APPROVED_DATE, null);
					if(head.get(FIELD_HEAD_SIGNED_BY) !=null){
						head.put(FIELD_HEAD_STATUS, DEFAULT_STATUS_SIGNED);
					}else{
						head.put(FIELD_HEAD_STATUS, DEFAULT_STATUS_UNSIGNED);
					}
					head.put("DEFAULT_STATUS_APPROVED", DEFAULT_STATUS_APPROVED);
					sqlSession.update(SQL_JOURNAL_UNAPPROVE,head);
				}
				sqlSession.commit();
				message.setResult(list);
			}
			writeMessage(context, message);
		}catch(Exception e){
			Integer errorId = Logger.log(ERROR_CODE_JOURNAL_UNAPPROVE, context, e);
			throw new SparkException(errorId);
		}finally {
			SessionFactory.closeSession(sqlSession);
		}
	}
}
